package srzy.driver

import srzy.data.PublicData
import srzy.common.Logging
import srzy.socket.DCCMsgActor
import srzy.module.CaseDeal
import srzy.common.Common
import srzy.diameter._
import srzy.socket._
import srzy.dao.DBUtil
import java.io.File
import srzy.common.Config

object Start6 extends Logging {

  def main(args: Array[String]): Unit = {
    deal
  }

  def deal = {

    def leftPad(str: String, i: Int, pad: Char = '0') = (List.fill(i)(pad).mkString("") + str).substring(str.length())

    val l = new java.io.File("case").listFiles().filter(x => x.isDirectory()).flatMap(x => x.listFiles())

    l.foreach(x => {

      val fl = Common.readFromFile(x).mkString("\n")
      val r0 = """<avp name="Calling-Vlr-Number"[ ]+value="([0-9A-Za-z]+)"></avp>""".r
      val r1 = """<avp name="Called-Vlr-Number"[ ]+value="([0-9A-Za-z]+)"></avp>""".r
      val r2 = """<avp name="MSC-Address"[ ]+value="([0-9A-Za-z]+)"></avp>""".r

      def numDeal(s: String) = if (s.slice(0, 2) == "86") s else "86" + s

      def f0(s: String) = r0.replaceAllIn(s, x => {
        val l = numDeal(x.group(1))
        """<avp name="Calling-Vlr-Number" value="""" + l + """"></avp>"""
      })
      def f1(s: String) = r1.replaceAllIn(s, x => {
        val l = numDeal(x.group(1))
        """<avp name="Called-Vlr-Number" value="""" + l + """"></avp>"""
      })
      def f2(s: String) = r2.replaceAllIn(s, x => {
        val l = numDeal(x.group(1))
        """<avp name="MSC-Address" value="""" + l + """"></avp>"""
      })

      val fn = f2(f1(f0(fl)))

      Common.writeToFile(x, fn)

      logger.debug("x:" + x.getAbsolutePath)

    })
  }
}

